package com.xujie.property_warranty_system.controller;

import com.xujie.property_warranty_system.common.Result;
import com.xujie.property_warranty_system.dto.FacilityDTO;
import com.xujie.property_warranty_system.dto.FacilityQueryDTO;
import com.xujie.property_warranty_system.dto.FacilityVO;
import com.xujie.property_warranty_system.dto.PageResult;
import com.xujie.property_warranty_system.service.FacilityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

/**
 * 设施设备控制器
 */
@RestController
@RequestMapping("/api/facilities")
@CrossOrigin(origins = "*")
public class FacilityController {
    
    @Autowired
    private FacilityService facilityService;
    
    /**
     * 分页查询设施设备
     */
    @GetMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE', 'OWNER')")
    public Result<PageResult<FacilityVO>> getFacilityPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String facilityType,
            @RequestParam(required = false) Boolean maintenanceSoon) {
        try {
            FacilityQueryDTO queryDTO = new FacilityQueryDTO();
            queryDTO.setPage(page);
            queryDTO.setSize(size);
            queryDTO.setKeyword(keyword);
            queryDTO.setStatus(status);
            queryDTO.setFacilityType(facilityType);
            queryDTO.setMaintenanceSoon(maintenanceSoon);
            
            PageResult<FacilityVO> result = facilityService.getFacilityPage(queryDTO);
            return Result.success(result);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取设施设备详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE', 'OWNER')")
    public Result<FacilityVO> getFacilityDetail(@PathVariable Long id) {
        try {
            FacilityVO facility = facilityService.getFacilityDetail(id);
            return Result.success(facility);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 添加设施设备
     */
    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE')")
    public Result<Long> addFacility(@Valid @RequestBody FacilityDTO facilityDTO) {
        try {
            Long id = facilityService.addFacility(facilityDTO);
            return Result.success(id);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 更新设施设备
     */
    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE')")
    public Result<String> updateFacility(@PathVariable Long id, @Valid @RequestBody FacilityDTO facilityDTO) {
        try {
            facilityDTO.setId(id);
            facilityService.updateFacility(facilityDTO);
            return Result.success("设备信息更新成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 删除设施设备
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN')")
    public Result<String> deleteFacility(@PathVariable Long id) {
        try {
            facilityService.deleteFacility(id);
            return Result.success("设备删除成功");
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取所有设备类型
     */
    @GetMapping("/types")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE', 'OWNER')")
    public Result<List<String>> getAllFacilityTypes() {
        try {
            List<String> types = facilityService.getAllFacilityTypes();
            return Result.success(types);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
    
    /**
     * 获取设备统计信息
     */
    @GetMapping("/statistics")
    @PreAuthorize("hasAnyRole('ADMIN', 'MAINTENANCE', 'OWNER')")
    public Result<FacilityService.FacilityStatisticsVO> getStatistics() {
        try {
            FacilityService.FacilityStatisticsVO statistics = facilityService.getStatistics();
            return Result.success(statistics);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
    }
}
